#include <stdio.h>
#include "texturefunctions.h"
#include <iostream>
#include <GL/glu.h>
using std::cout;

/*converts big endian to little endian short data to use SGI files on a PC*/
unsigned short byteSwapShort(unsigned short num)
{
return(((num>>8)&0x00FF)|((num<<8)&0xFF00));

}


RGBImage *LoadRGB(const char *filename)
{
  FILE *ifp;
  RGBHeader *header;
  char *fileImgData;
  long width,height,depth;
  int i,j,k;
  RGBImage *img;

  cout << "Oeffne Datei: " << filename << "\n";
  ifp=fopen(filename,"r");
  if(ifp==NULL)
    {
      cout << "Datei " << filename << "---------------------------> nicht vorhanden.\n";
      return 0;
    }
    cout << "Datei " << filename << " vorhanden.\n";


/*load header*/
  header=new RGBHeader();
  fread(header,1,sizeof(RGBHeader),ifp);


  width=header->xsize;
  height=header->ysize;
  depth=header->zsize;

  width=byteSwapShort(width);
  height=byteSwapShort(height);
  depth=byteSwapShort(depth);


  /*load image from file*/
  //fileImgData=(char *)safemalloc((float)(1.0*width*height*depth));
  fileImgData= new char[(1*width*height*depth)];
  fread(fileImgData,1,width*height*depth,ifp);

  /*setup image*/
  //img=(RGBImage *)safemalloc(sizeof(RGBImage));
  img = new RGBImage();

  img->width=width;
  img->height=height;
  img->depth=depth;
  img->imgData=new char[(width*height*depth)];

  /*convvert data from file format into gl friendly format*/
  for(j=0;j<height;j++)
    for(i=0;i<width;i++)
      for(k=0;k<depth;k++)
	{
	  img->imgData[(j*width+i)*depth+k]=
		fileImgData[(k*height+j)*width+i];
	}

  //free(fileImgData);
  //free(header);
  delete[] fileImgData;
  delete header;
  return(img);
}


void OGL_Set_Current_Material_Parameters(float shininess, float r_diffuseColor, float g_diffuseColor, float b_diffuseColor, float r_specularColor, float g_specularColor, float b_specularColor)
{
	GLfloat glfModelSpecular[4];

	// This causes the polygon's ambient & diffuse colors to be derived from the glColor() value.
	glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
	glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
	/* Set the specular shading color. */
	glfModelSpecular[0] = r_specularColor;
	glfModelSpecular[1] = g_specularColor;
	glfModelSpecular[2] = b_specularColor;
	glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, glfModelSpecular);
	glColor4f(r_diffuseColor, g_diffuseColor, b_diffuseColor,1);
}

void OGL_Set_Current_Texture_Map(const RGBImage *img, float u_scale, float v_scale, float u_offset, float v_offset)
{
  glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,img->width,img->height,0,GL_RGB,
  GL_UNSIGNED_BYTE,img->imgData);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  // glShadeModel(GL_FLAT);
}

/*GLuint loadMipmappedTexture(RGBImage *img) {
    GLuint textureId;
    glGenTextures(1, &textureId);
    cout << "Texture-id ist : " << textureId << "\n";
    glBindTexture(GL_TEXTURE_2D, textureId);
    cout << "Textur gebunden.\n";
    cout << "Fehler beim generieren von Mipmaps: " << gluBuild2DMipmaps(GL_TEXTURE_2D,
                      GL_RGB,
                      img->width, img->height,
                      GL_RGB,
                      GL_UNSIGNED_BYTE,
		      img->imgData) << "\n";
    return textureId;
}

void setMipmappedTexture(GLuint texname)
{
  glBindTexture(GL_TEXTURE_2D, texname);


  }*/
